//
//  RegisterGroupViewController.swift
//  placepic
//
//  Created by elesahich on 2020/09/05.
//  Copyright © 2020 elesahich. All rights reserved.
//

import UIKit

class GroupCodeViewController: UIViewController {
    
    @IBOutlet weak var groupCodeLabel: UILabel!
    @IBOutlet weak var inviteCodeTextField: UITextField!
    @IBOutlet weak var explainLabel: UILabel!
    @IBOutlet weak var registerGroupButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMainView()
        setupNavigationBar()
        addTargetToTextFields()
    }
    @IBAction func nextButtonAction(_ sender: Any) {
        guard let vc = storyboard?.instantiateViewController(identifier: "ProfileInsertViewController") as? ProfileInsertViewController else {
            return
        }
        navigationController?.pushViewController(vc, animated: true)
    }
}


extension GroupCodeViewController {
    
    private func addTargetToTextFields() {
        inviteCodeTextField.addTarget(self,
                                      action: #selector(changeButtonActions),
                                      for: .allEditingEvents)
    }
    
    @objc
    func changeButtonActions() {
        if inviteCodeTextField.hasText {
            inviteCodeTextField.layer.borderColor = UIColor.gray70.cgColor
            inviteCodeTextField.layer.borderWidth = 1.5
            inviteCodeTextField.backgroundColor = UIColor.white
            registerGroupButton.backgroundColor = UIColor.warmPink
            registerGroupButton.layer.borderColor = UIColor.warmPink.cgColor
            registerGroupButton.layer.borderWidth = 1.5
            registerGroupButton.setTitle("그룹 들어가기", for: .normal)
            registerGroupButton.setTitleColor(UIColor.white, for: .normal)
        } else {
            inviteCodeTextField.backgroundColor = UIColor.blueGray10
            inviteCodeTextField.layer.borderColor = UIColor.blueGray10.cgColor
            registerGroupButton.backgroundColor = UIColor.blueGray10
            registerGroupButton.layer.borderColor = UIColor.blueGray10.cgColor
            registerGroupButton.setTitleColor(UIColor.gray70, for: .normal)
        }
    }
    
    private func setupMainView() {
        groupCodeLabel.text = "그룹 코드"
        groupCodeLabel.font = UIFont.boldSystemFont(ofSize: 15)
        groupCodeLabel.textColor = UIColor.gray90
        
        let attributedString = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 15),
                                NSAttributedString.Key.foregroundColor: UIColor.blueGray30,
        ]
        inviteCodeTextField.setLeftPaddingPoints(10)
        inviteCodeTextField.attributedPlaceholder = NSAttributedString(string: "초대 코드를 입력해주세요", attributes: attributedString)
        inviteCodeTextField.backgroundColor = UIColor.blueGray10
        inviteCodeTextField.layer.cornerRadius = 8
        inviteCodeTextField.layer.borderColor = UIColor.blueGray10.cgColor
        inviteCodeTextField.layer.borderWidth = 1
        inviteCodeTextField.clipsToBounds = true
        inviteCodeTextField.delegate = self
        
        explainLabel.font = UIFont.systemFont(ofSize: 9)
        explainLabel.textColor = UIColor.blueGray80
        explainLabel.text = "그룹 코드는 SOPT 27기 페이스북 페이지에서 확인하실 수 있습니다."
        
        registerGroupButton.layer.cornerRadius = 8
        registerGroupButton.backgroundColor = UIColor.blueGray10
        registerGroupButton.setTitle("그룹 가입하기", for: .normal)
        registerGroupButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        registerGroupButton.setTitleColor(UIColor.gray70, for: .normal)
    }
    
    private func setupNavigationBar() {
        guard let navigationBar = self.navigationController?.navigationBar else { return }
        
        navigationBar.isTranslucent = true
        navigationBar.backgroundColor = UIColor.clear
        navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        navigationBar.shadowImage = UIImage()
        
        let title: UILabel = {
            let label = UILabel()
            label.text = "회원가입/로그인"
            label.font = .boldSystemFont(ofSize: 18)
            label.textColor = UIColor.gray90
            return label
        }()
        
        let backButton: UIButton = {
            let bt = UIButton()
            bt.setImage(UIImage(named: "backArrowIc"), for: .normal)
            return bt
        }()
        
        backButton.addTarget(self,
                             action: #selector(dismissVC),
                             for: .touchUpInside)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
        navigationItem.titleView = title
    }
    
    @objc
    private func dismissVC() {
        self.navigationController?.popViewController(animated: true)
    }
}

extension GroupCodeViewController: UITextFieldDelegate {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
    }
}
